<!-- 用户 -->

<template>
  <FormSlot label="负责人" bind="userId" tip="请选择负责人" required>
    <div>
      <span v-if="one.userId" class="el-button--text" @click="toOpen">
        {{ one.userName }}{{ one.userPhone ? `(${one.userPhone})` : '' }}
      </span>

      <span v-if="!isShow">
        <BtnText v-if="one.userId" title="清除" @click="toClear" />
        <BtnPrimary v-else title="选择" @click="toOpen" />
      </span>
    </div>

    <EasyModal v-model="easyVisible" title="选择负责人" medium @submit="toSubmit" @close="toReset">
      <ChooseUserOne v-bind="chooseProps" />
    </EasyModal>
  </FormSlot>
</template>

<script>
// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'

const EMPTY = { uaaUserId: '', name: '', phone: '' }

// ============================== 导出组件 ============================== //

export default {
  /**
   * 名称定义 (实际使用名称)
   */
  name: 'MyUser',

  /**
   * 混入列表 (配置选项合并)
   */
  mixins: [CORE.mixWeb()],

  /**
   * 状态数据 (函数返回对象)
   */
  data() {
    return {
      easyVisible: false, // 弹窗是否可见
      obj: { user: CORE.clone(EMPTY) }, // 数据对象
    }
  },

  /**
   * 计算属性 (慎用箭头函数)
   */
  computed: {
    /**
     * 选择弹窗的属性
     */
    chooseProps() {
      const { obj, one: { planType }, DICT: { TYPE } } = this
      const scope = CORE.dict(TYPE, planType, 2)
      return { obj, bind: 'user', [scope]: true }
    },
  },

  /**
   * 监听属性 (深度 deep 首次 immediate)
   */
  watch: {
    /**
     * 监听弹窗可见
     */
    visible(visible) {
      visible && this.toReset()
    },
  },

  /**
   * 本地方法 (轻量重要在前)
   */
  methods: {
    /**
     * 弹窗
     */
    toOpen() {
      this.easyVisible = true
    },

    /**
     * 提交
     */
    toSubmit() {
      const { obj, one } = this
      one.userId = obj.user.uaaUserId
      one.userName = obj.user.name
      one.userPhone = obj.user.phone
      this.easyVisible = false
    },

    /**
     * 重置
     */
    toReset() {
      const { userId: uaaUserId, userName: name, userPhone: phone } = this.one
      this.obj = { user: { uaaUserId, name, phone } }
    },

    /**
     * 清除
     */
    toClear() {
      this.one.userId = ''
      this.one.userName = ''
      this.one.userPhone = ''
      this.obj = { user: CORE.clone(EMPTY) }
    },
  },
}
</script>
